package anveshitha.config;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Elements;
import nu.xom.ParsingException;
import nu.xom.ValidityException;

/**
 * Contains the anveshitha constants
 * @author varun
 */
public class AnveshithaConstants {

	// constants
	public static String ENGLISH;
	public static String JAVA;
	public static String LUCENE_INDEX_DIR;
	public static String CONFIG_PROPERTIES;
	public static String FILE_SEPARATOR = System.getProperty("file.separator");
	
	// field constants
	public static enum FieldEnum {
		CLASSNAME, IMPORT, METHODNAME, METHODCODE, FILENAME, METHODPARAMTYPE, RETURNTYPE, COMMENTS, EXTENDS, IMPLEMENTS, PACKAGENAME, FILEPATH
	};
	
	//timer delays
	public static HashMap<String,Long> TIMER_DELAYS;
	
	// stop words for code analyzers
	public static String[] JAVA_STOP_WORDS;
	public static String[] ENGLISH_STOP_WORDS;

	// directory lists
	public static String[] JAVA_SRC_DIRS;

	// index counter
	public static int indexFileCounter = 0;

	/**
	 * This method loads constants from the config xml
	 * @throws Exception 
	 */
	public static void loadConfigData(String configXml) throws Exception
	{
		if(configXml == null)
			throw new Exception("Config XML Null");
		
		//parse the xml and build a doc object
		Builder xmlBuilder = new Builder();
		Document xmlDoc = xmlBuilder.build(new File(configXml));
		
		//get root element
		Element xmlRoot = xmlDoc.getRootElement();
		
		//get all constants
		HashMap<String, String> constantsMap = getConstantsHashMap(xmlRoot);
		LUCENE_INDEX_DIR = constantsMap.get("LUCENE_INDEX_DIR");
		CONFIG_PROPERTIES = constantsMap.get("CONFIG_PROPERTIES");
		JAVA = constantsMap.get("JAVA");
		ENGLISH = constantsMap.get("ENGLISH");
		
		//get the timers delay
		TIMER_DELAYS = getTimersHashMap(xmlRoot);		
		
		//get the stop sets
		JAVA_STOP_WORDS = getSet(xmlRoot, "stopset","word",JAVA);
		ENGLISH_STOP_WORDS = getSet(xmlRoot,"stopset","word", ENGLISH);
		
		//get the source directories also replace file separators
		JAVA_SRC_DIRS = getSet(xmlRoot, "source","directory",JAVA);

	}

	/**
	 * Goes through xml and gets the data as a string array
	 * @param xmlRoot
	 * @param parentTag
	 * @param childTag
	 * @param language
	 * @return
	 */
	private static String[] getSet(Element xmlRoot,String parentTag,String childTag,String language) {
		
		//first get all the stopsets and make a map by language
		// <parents><parent language=""></parent><parents>
		Elements sets = xmlRoot.getFirstChildElement(parentTag+"s").getChildElements(parentTag);
		HashMap<String, Elements> setsMap = new HashMap<String, Elements>();
		Element stopset;
		for(int index = 0;index < sets.size();index++)
		{
			stopset = sets.get(index);
			setsMap.put(stopset.getAttributeValue("language"),stopset.getChildElements(childTag));
		}
		
		
		//get all the child tags by language
		//<child></child>
		Elements children = setsMap.get(language); 
		Element child;
		Collection<String> setList = new ArrayList<String>();
		for(int index = 0;index < children.size();index++)
		{
			child = children.get(index);
			setList.add(child.getValue());			
		}
		
		String set[] = new String[setList.size()];
		setList.toArray(set);
		
		return set;
	}

	/**
	 * gets the constants hashmap from xml
	 * @param xmlRoot
	 * @return
	 */
	private static HashMap<String, String> getConstantsHashMap(Element xmlRoot) {
		Elements constants = xmlRoot.getFirstChildElement("constants").getChildElements("constant");
		HashMap<String, String> constantsMap = new HashMap<String, String>();
		Element constant;
		for(int index = 0;index < constants.size();index++)
		{
			constant = constants.get(index);
			constantsMap.put(constant.getAttributeValue("name"),constant.getValue());
		}
		
		return constantsMap;
	}
	
	/**
	 * gets the timers hashmap from xml
	 * @param xmlRoot
	 * @return
	 */
	private static HashMap<String, Long> getTimersHashMap(Element xmlRoot) {
		Elements timers = xmlRoot.getFirstChildElement("timers").getChildElements("delay");
		HashMap<String, Long> delayMap = new HashMap<String, Long>();
		Element delay;
		for(int index = 0;index < timers.size();index++)
		{
			delay = timers.get(index);
			delayMap.put(delay.getAttributeValue("language"),new Long(delay.getValue()+"000")); //convert seconds to milliseconds
		}
		return delayMap;
	}

}
